//
//  ToBindCompanyViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/9/15.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//

#import "ToBindCompanyViewController.h"
#import "HttpRequest.h"
#import "RDVTabBarController.h"
#import "UIViewExt.h"
#import "ZJSwitch.h"
#import "BindCompanyViewController.h"
@interface ToBindCompanyViewController ()<UITextFieldDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UIScrollViewDelegate>
@property (retain,nonatomic)UIImageView *recordBtn;
@end

@implementation ToBindCompanyViewController

{
    MBProgressHUD *progressHud;
     UIButton* login;
}
@synthesize alt = alt;
@synthesize mDelegate;
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
    
}
-(void)BackAction:(UIButton *)sender{
    
     [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@""];
    CGRect backframe = CGRectMake(0,0,23/2,41/2);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
}
-(void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"导购认证";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     backColor,
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    [self initView];
    
    
    
}

- (void)initView
{
    UIView* myToolBar=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, viewheight)];
    myToolBar.backgroundColor=jianpancolor;
    UIButton*  tubtnDone=[[UIButton alloc]initWithFrame:CGRectMake(logoX,logoY, logoheight, logowidth)];
    [tubtnDone setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [tubtnDone addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:tubtnDone];
    UIButton*btnDone111=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width - 55, 12, 40, 16)];
    [btnDone111 setTitle:@"确定" forState:UIControlStateNormal];
    [btnDone111 setTitleColor:jianpancolor1 forState:UIControlStateNormal];
    btnDone111 .titleLabel .font=[UIFont systemFontOfSize:keyboardFot];
    [btnDone111 addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:btnDone111];
    myToolBar.alpha=alp;
    UIButton* dabtn=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width-60, 0, 60, 40)];
    [dabtn addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:dabtn];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    _userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(20,60/2+64, ScreenWidth-40,44)];
    [_userNameTextField setBorderStyle:   UITextBorderStyleNone]; //外框类型
    _userNameTextField.placeholder = @"请输入姓名"; //默认显示的字
    _userNameTextField.font = [UIFont systemFontOfSize: 14];
    _userNameTextField.textColor= [UIColor blackColor];
    _userNameTextField.tag=22;
    [[UIColor whiteColor] setFill];
    _userNameTextField.returnKeyType = UIReturnKeyDone;
    _userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    _userNameTextField.delegate = self;
    [self.view addSubview:_userNameTextField];
    
    _phoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(20,150/2+64, ScreenWidth-50, 44)];
    [_phoneTextField setBorderStyle:   UITextBorderStyleNone]; //外框类型
    _phoneTextField.placeholder = @"请输入店长手机号码"; //默认显示的字
    _phoneTextField.font = [UIFont systemFontOfSize: 14];
    _phoneTextField.textColor= [UIColor blackColor];
    _phoneTextField.keyboardType = UIKeyboardTypeDecimalPad;
    _phoneTextField.autocorrectionType = UITextAutocorrectionTypeNo;
    _phoneTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    _phoneTextField.returnKeyType = UIReturnKeyDone;
    [_phoneTextField addTarget:self action:@selector(phoneTextFieldLength:) forControlEvents:UIControlEventEditingChanged];
    _phoneTextField.delegate = self;
    [self.view addSubview:_phoneTextField];
    ZJSwitch *switch2 = [[ZJSwitch alloc] initWithFrame:CGRectMake(ScreenWidth-75, 98/2+10+44, 55, ScreenHeight*15/1334)];
    switch2.onText = @"男";
    switch2.offText = @"女";
    switch2.on = YES;
    switch2.backgroundColor = [UIColor clearColor];
    switch2.onTintColor = [UIColor colorWithRed:53/255.0 green:159/255.0 blue:234/255.0 alpha:1];
    switch2.tintColor = [UIColor colorWithRed:253.00/255.0 green:129.00/255.0 blue:182.00/255.0 alpha:1];
    self.sex= @"男";
    [switch2 addTarget:self action:@selector(switchAction1:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:switch2];

    _phoneTextField.inputAccessoryView = myToolBar;
    _userNameTextField.inputAccessoryView = myToolBar;
    [self loginBtn];
    UILabel *line1 = [[UILabel alloc]initWithFrame:CGRectMake(15,148/2+64, ScreenWidth-30, 0.25)];
    line1.backgroundColor = qianhuiColor;
    [self.view addSubview:line1];
    UILabel *line2 = [[UILabel alloc]initWithFrame:CGRectMake(15,236/2+64, ScreenWidth-30, 0.25)];
    line2.backgroundColor = qianhuiColor;
    [self.view addSubview:line2];

    }
-(void)switchAction1:(UISwitch*)sender{
    
    if (sender.on ==YES) {
        self.sex=nil;
        self.sex=@"男";
    }
    if (sender.on == NO) {
        self.sex=nil;
        self.sex=@"女";
    }
}
#pragma mark Event Handle

-(void)login:(UIButton *)sender{
    
    if ([_userNameTextField.text length] <1)
    {
        [self showAlertMessage:@"请输入姓名"];
    }
    else{
        
        
        if ([_phoneTextField.text length] < 1)
        {
            [self showAlertMessage:@"请输入店长手机号码"];
        }
        else
        {
            [Global showIndeterminateHud:@"登录中，请稍后..." delay:30 view:self.view];
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSString* Token = [user objectForKey:@"tokenId"];
            NSMutableDictionary *params = [NSMutableDictionary new];
            [params setValue:Token forKey:@"tokenId"];
            [params setValue:_userNameTextField.text forKey:@"userName"];
            [params setValue:_phoneTextField.text forKey:@"phone"];
            [params setValue:self.sex forKey:@"sex"];
            [HttpRequest requestData1:@"Common/ToBindCompany" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                if (isSuccess) {
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                        
                        [Global hideProgressHUDInternal:self.view];
                        BindCompanyViewController *generateVC=[[BindCompanyViewController alloc] init];
                        generateVC.DataLine=[severDataDict.dataDict objectForKey:@"DataLine"];
                       [self.navigationController pushViewController:generateVC animated:YES];

                    }else{
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                        [Global showTextHudSub:requestResultStr view:self.view];
                        
                    }}
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    [Global showTextHudSub:@"连接失败" view:self.view];
                }
                
            }];
        }}}
- (void)phoneTextFieldLength:(UITextField*)textField
{
    
    if ([textField.text length] > 11)
    {
        textField.text = [textField.text substringToIndex:11];
    }
    if ([textField.text length] > 2)
    {
        [self loginBtn];
    }
}
-(void)loginBtn{
    [login removeFromSuperview];
    login = [[UIButton alloc] initWithFrame:CGRectMake(10, 525/2, ScreenWidth-20, anniuHeigth)];
    [login setTitle:@"登录" forState:UIControlStateNormal];
    [login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    if (_phoneTextField.text.length == 0) {
        
        [login.layer setCornerRadius:3.0]; //设置矩圆角半径
        [login.layer setBorderWidth:0.3];   //边框宽度
        CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
        CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){228/255.0, 228/255.0, 228/255.0, 1.0});
        [login.layer setBorderColor:tureBTncolorref];//边框颜色
        login.backgroundColor=qianhuiColor;
        [self.view addSubview:login];
    }if (_phoneTextField.text.length >1) {
        
        login.backgroundColor=luseColor;
        [login.layer setCornerRadius:3.0]; //设置矩圆角半径
        [login.layer setBorderWidth:0.3];   //边框宽度
        CGColorSpaceRef tureBTnSpace = CGColorSpaceCreateDeviceRGB();
        CGColorRef tureBTncolorref = CGColorCreate(tureBTnSpace,(CGFloat[]){23/255.0, 148/255.0, 152/255.0, 1.0});
        [login.layer setBorderColor:tureBTncolorref];//边框颜色
        [login addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:login];
    }
}
-(void)OnTapDone{
    [_userNameTextField resignFirstResponder];
    [_phoneTextField resignFirstResponder];
    [self loginBtn];

}



- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定哦 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
